<?php
class Front_BlogController extends BasePage implements IController 
{
	/**
	 * 构造函数。
	 *
	 */
	function __construct() {
		parent::__construct();
	}
	
	/**
	 * 控制转发器。
	 *
	 */
	function dispatcher() {
		if ($this->isPostback) {
			if (!$this->isLogined) {
				$this->go('login', '');
			}
		
			switch ($this->action) {
				case 'add':
					$this->addBlog();
					break;
			}
		} else {
			switch ($this->view) {
				case 'add':
					include (DOC_ROOT . 'fckeditor' . DS . 'fckeditor.php');
					
					$fckeditor = new FCKeditor('content');
					$fckeditor->BasePath = '/blog/fckeditor/';
					$fckeditor->ToolbarSet = 'BasicMe';
					$fckeditor->Width = 720;
					$fckeditor->Height = 320;
					$this->assign('fckeditor_content', $fckeditor->CreateHtml());
					
					$this->assign('categories', $this->db->fetchAll("SELECT * FROM `tbg_categories`"));
					$this->display('blog_add');
					break;
				default:
					$count = $this->db->scalar("SELECT COUNT(*) FROM `tbg_blogs` WHERE `permission` = 1");
					$arr_1 = $this->db->getPages($count, 10, 'offset', 2);
					$this->assign('blogs', $this->db->fetchAll("SELECT * FROM `tbg_blogs` WHERE `permission` = 1 ORDER BY `addtime` DESC LIMIT " . $arr_1['StartIndex'] . "," . $arr_1['PageSize']));
					$this->assign('pages', $arr_1);
					$this->display('blog');					
					break;
			}
		}
	}
	
	/**
	 * 发表日志。
	 *
	 */
	function addBlog() {
		if (!$this->isLogined) {
			$this->showErrors('您尚未登录或会话已超时!');
		}
		
		$this->db->insert("INSERT INTO `tbg_blogs`(`user_id`, `cat_id`, `title`, `content`, `permission`, `addtime`) VALUES(?, ?, ?, ?, ?, ?)", array(
			$this->session->UserId,
			( int ) $this->forms['classid'],
			$this->forms['title'],
			$this->forms['content'],
			( int ) $this->forms['permission'],
			$this->currentTime
		));
		
		$this->go('blog', '');
	}
}